// @charset "utf-8";

define(function(require, exports, module) {
	var clientApi = require('../public/clientapi'),
		publicMethod = require('../public/public');

	var voteData = require('./votelog');

	var voteModule = {
		init: function() {
			var self = this;
			self.getRiseFallCount();
			self.voteAutoRequest();
			$('#voteRiseBtn').on('click', function() {
				if (!$(this).hasClass('vote-rise-selected')) {
					self.voting('1');
				}
			});
			$('#voteFallBtn').on('click', function() {
				if (!$(this).hasClass('vote-fall-selected')) {
					self.voting('-1');
				}
			});

		},
		getTradeDayAndNext: function(callbackfn) {
			var serverTime = clientApi.serverTime();
			var serverHour = clientApi.serverHour();
			publicMethod.ajaxJsonp({
				url: 'http://sp.10jqka.com.cn/api/tradeday/get/date/' + serverTime + '/offset/3/isrange/1/return/jsonp/callback/gettradeday/',
				jsonpCallback: 'gettradeday',
				callback: function(result) {
					var tradeDay, nextTradeDay;
					if (result.errorcode === 0) {
						if (serverHour >= '1500' && serverTime >= result.result[0]) {
							tradeDay = result.result[1];
							nextTradeDay = result.result[2];
						} else {
							tradeDay = result.result[0];
							nextTradeDay = result.result[1];
						}
						callbackfn(tradeDay, nextTradeDay);
					}
				}
			});
		},

		voteTimer: null,
		voteAutoRequest: function() {
			var self = this;
			self.voteTimer = setInterval(function() {
				self.getRiseFallCount();
			}, 30000);
		},
		clearVoteTimer: function() {
			var self = this;
			clearInterval(self.voteTimer);
		},
		theTradeDay: '',
		getRiseFallCount: function() {
			var self = this;
			self.getTradeDayAndNext(function(tradeDay, nextTradeDay) {
				if (self.theTradeDay === '') {
					self.theTradeDay = tradeDay;
				}
				self.reqRiseFallCount(tradeDay);
			});
		},
		riseCount: 0,
		fallCount: 0,
		reqRiseFallCount: function(tradeDay) {
			var self = this;
			$('#voteDate').text(tradeDay.substr(0, 4) + '-' + tradeDay.substr(4, 2) + '-' + tradeDay.substr(6, 2) + ' ');

			publicMethod.ajaxJsonp({
				url: 'http://sp.10jqka.com.cn/api/evaluation/get/app/7/return/jsonp/',
				jsonpCallback: 'callback',
				data: {
					key: tradeDay
				},
				callback: function(result) {
					if (result.errorcode === 0) {
						self.riseCount = result.result.supports;
						self.fallCount = result.result.oppositions;
						self.calcVoteRatio(self.riseCount, self.fallCount);

						self.checkIsNext(tradeDay);
					}
				}

			});
		},
		calcVoteRatio: function(riseNum, fallNum) {
			var riseWidth = 0;
			if (riseNum + fallNum != 0) {
				riseWidth = Math.floor(riseNum / (riseNum + fallNum) * 100);
			}
			$('#voteProgressRise').animate({
				width: riseWidth + '%'
			});
			$('#voteRiseNum').text(riseNum);
			$('#voteFallNum').text(fallNum);
		},
		checkIsNext: function(tradeDay) {
			var self = this;
			if (tradeDay > self.theTradeDay) {
				voteData.saveVoteData('0');
				self.theTradeDay = tradeDay;
			}
			var voteText = voteData.getVoteData();
			if (voteText == 1) {
				$('#voteFallBtn').removeClass('vote-fall-selected');
				$('#voteRiseBtn').addClass('vote-rise-selected');
				$('#voteModule').addClass('already-vote');
			} else if (voteText == 2) {
				$('#voteRiseBtn').removeClass('vote-rise-selected');
				$('#voteFallBtn').addClass('vote-fall-selected');
				$('#voteModule').addClass('already-vote');
			} else if (voteText == 0) {
				$('#voteFallBtn').removeClass('vote-fall-selected');
				$('#voteRiseBtn').removeClass('vote-rise-selected');
				$('#voteModule').removeClass('already-vote');
			}
		},

		// 投票
		voting: function(operate) {
			var self = this;
			self.getTradeDayAndNext(function(tradeDay, nextTradeDay) {
				// self.theTradeDay = tradeDay;
				self.getToken(tradeDay, operate);
			});
		},
		getToken: function(tradeDay, operate) {
			var self = this;
			publicMethod.ajaxJsonp({
				url: 'http://sp.10jqka.com.cn/api/evaluation/gettoken/app/7/return/jsonp/',
				jsonpCallback: 'callback',
				data: {
					key: tradeDay
				},
				callback: function(result) {
					if (result.errorcode === 0) {
						var token = result.result;
						self.goToVote(tradeDay, token, operate);
					}
				}
			});

		},
		goToVote: function(tradeDay, token, operate) {
			var self = this;

			publicMethod.ajaxJsonp({
				url: 'http://sp.10jqka.com.cn/api/evaluation/save/app/7/return/jsonp/',
				jsonpCallback: 'callback',
				data: {
					key: tradeDay,
					token: token,
					operate: operate
				},
				callback: function(result) {
					if (result.errorcode === 0) {
						if (operate == 1) {
							$('#voteRiseBtn').addClass('vote-rise-selected');
							voteData.saveVoteData('1');
						} else if (operate == -1) {
							$('#voteFallBtn').addClass('vote-fall-selected');
							voteData.saveVoteData('2');
						}
						self.reqRiseFallCount(tradeDay);
					} else {
						// alert(result.errormsg);
					}
				}
			});
		}
	};

	module.exports = voteModule;
});